<?php
	include ('lib/twitese.php');
	$title = "Settings";
	include ('inc/header.php');	
	if (!loginStatus()) header('location: login.php');	
?>
<script src="js/colorpicker.js"></script>
<script src="js/setting.js"></script>
<link rel="stylesheet" href="css/colorpicker.css" />
<div id="statuses" class="column round-left">
	<div id="setting">
<?php 
	$t = getTwitter();
	$user = $t->veverify();
	
	$settingType = isset($_GET['t'])? $_GET['t'] : 1;
	if (isset($_POST['name'])) {
		$args = array();
		$args['name'] = $_POST['name'];
		$args['url'] = $_POST['url'];
		$args['location'] = $_POST['location'];
		$args['description'] = $_POST['description'];
		$result = $t->updateProfile($args);
		if ($result) echo "<div id=\"otherTip\">你的资料已被更新!</div>";
		else echo "<div id=\"otherTip\">更新失败请重试.</div>";
	}
	
		if(isset($_GET['t'])){
		$mode = $_GET['t'];
		switch($mode){
			case 'avatar':
			 //// -- 设置头像 --//
			 if(! empty($_FILES)){
				 	$params = array(
	  				'image' => "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
					);
					if(update_Avatar($t,$params))
						echo "<div id=\"otherTip\">上传头像成功!</div>";
					else
						echo "<div id=\"otherTip\">上传头像失败!</div>";
				 }
			else{
				echo "<div id=\"otherTip\">未选择头像文件!</div>";
			}
			 break;
			 
			 case 'background':
			 // ---上传背景 --//
			 	if(! empty($_FILES)){
					$params = array(
	 				 'image' => "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
					);
					if(update_background($t,$params))
						echo "<div id=\"otherTip\">上传背景成功!</div>";
					else
						echo "<div id=\"otherTip\">上传背景失败!</div>";
				}
				else
					echo "<div id=\"otherTip\">没有选择背景图片!</div>";
			 break;
			 
			 case 'color':
			 /// -- 设置风格颜色 --///
			 
			 if(isset($_POST['profile_background_color']))
			 	$profile_background_color = $_POST['profile_background_color'];
			 else
			 	$profile_background_color = $user->profile_background_color;
				
			 if(isset($_POST['profile_text_color']))
			 	$profile_text_color = $_POST['profile_text_color'];
			 else
			 	$profile_text_color = $user->profile_text_color;
				
			 if(isset($_POST['profile_link_color']))
			 	$profile_link_color = $_POST['profile_link_color'];
			 else
			 	$profile_text_color = $user->profile_link_color;
			
			 if(isset($_POST['profile_sidebar_fill_color']))
			 	$profile_sidebar_fill_color= $_POST['profile_sidebar_fill_color'];
			 else
			 	$profile_sidebar_fill_color = $user->profile_sidebar_fill_color;
				
			 if(isset($_POST['profile_sidebar_border_color']))
			 	$profile_sidebar_border_color = $_POST['profile_sidebar_border_color'];
			 else
			 	$profile_sidebar_border_color = $user->profile_sidebar_border_color;
				
				update_profile_colors($t,
					$profile_background_color,
					$profile_text_color,
					$profile_link_color,
					$profile_sidebar_fill_color,
					$profile_sidebar_border_color);
			echo "<div id=\"otherTip\">保存风格配色方案成功!</div>";
			 break;
			}
		}
?>
<div id="setting_nav">
<?php
	switch($settingType){
		case 'profile':
		case 'avatar':
?>
			<span class="subnavLink"><a href="setting.php">浏览/风格设置</a></span><span class="subnavNormal">资料设置</span><span class="subnavLink"><a href="setting.php?t=advanced">高级</a></span>
<?php			
			break;
		case 'advanced':
?>
			<span class="subnavLink"><a href="setting.php">浏览/风格设置</a></span><span class="subnavLink"><a href="setting.php?t=profile">资料设置</a></span><span class="subnavNormal">高级</span>
<?php		
			break;
		default:
?>
			<span class="subnavNormal">浏览/风格设置</span><span class="subnavLink"><a href="setting.php?t=profile">资料设置</a></span><span class="subnavLink"><a href="setting.php?t=advanced">高级</a></span>
<?php	
	} // end switch
?>
</div>
<?php
	switch($settingType){
		case 'profile':
		case 'avatar':
			$user = getTwitter()->veverify();
?>
			<form id="setting_form" action="setting.php?t=profile" method="post">
				<table id="setting_table">
				<tr>
				<td class="setting_title">昵称：</td>
				<td><input class="setting_input" type="text" name="name" value="<?php echo isset($user->name) ? $user->name : ''?>" /></td>
				</tr>
				<tr>
				<td class="setting_title">网站：</td>
				<td><input class="setting_input" type="text" name="url" value="<?php echo isset($user->url) ? $user->url : '' ?>" /></td>
				</tr>
				<tr>
				<td class="setting_title">地址：</td>
				<td><input class="setting_input" type="text" name="location" value="<?php echo isset($user->location) ? $user->location : '' ?>" /></td>
				</tr>
				<tr>
				<td class="setting_title">简介：</td>
				<td><textarea id="setting_text" name="description"><?php echo isset($user->description) ? $user->description : '' ?></textarea><small style="margin-left:5px;vertical-align: top;">*最多可是输入160个字</small></td>
				</tr>
				<tr>
				<td colspan="2"><input type="submit" class="btn" style="margin-left:62px !important" id="save_button" value="保存"></input></td>
				</tr>
				</table>
				</form>
            <fieldset class="settings">

			<legend>上传头像</legend>
            <form action="setting.php?t=avatar" method="POST" enctype="multipart/form-data">
            <table width="397" height="73" border="0">
 			 <tr>
    			<td width="73"><img id="img_Preview"width="73" height="73" src="<?php echo $user->profile_image_url; ?>"/></td>
   				 <td>
                 <input type="file" name="image" id="imgFile"/>
                 <p>图片最大只能为 700k. JPG, GIF, PNG.<input type="submit" class="btn" id="avatar_button" value="上传" />
                 </p>
                 </td>
  			</tr>
			</table>
            </form>
            <script language="jscript">
				imgFile.addEventListener('change',readFile,false );
				function readFile(){
        			var file = this.files[0];
        			if(!/image\/\w+/.test(file.type)){
               		 alert("请确保文件为图像类型");
                	return false;
        			}
       				 var reader = new FileReader();
        				reader.readAsDataURL(file);
        				reader.onload = function(e){
               		 	img_Preview.src=this.result;
       					}
					}
			</script>
      </fieldset>
<?php
			break;
		case 'advanced':
		
?>
		<form id="style_form" action="setting.php?t=advanced" method="post">
			<fieldset class="settings">

			<legend>代理</legend>

			<input id="proxifyAvatar" type="checkbox" />
			<label>通过代理显示头像</label>
			<br />

			</fieldset>
			
			<table>
			<tr>
			<td colspan="2">
			<input type="submit" class="btn" id="save_button" value="保存" />
			</td>
			</tr>
			</table>
		</form>

<?php
			break;
		default:
		$time = $_SERVER['REQUEST_TIME']+3600*24*365;
			if ( isset($_POST['fontsize']) ) {
				try {
					saveStyle($_POST['myCSS'], $_POST['fontsize'], $_POST['bodyBg']);
					echo "<div id=\"otherTip\">Your styles have been updated!</div>";
				} catch (Exception $e) {
					echo "<div id=\"otherTip\">Update failed. Please try again.</div>";
				}
			}
			if(isset($_POST['homeInterval'])){
				setcookie('homeInterval', $_POST['homeInterval'], $_SERVER['REQUEST_TIME']+3600*24*365, '/');
				setcookie('intervalChanged', 'true', $_SERVER['REQUEST_TIME']+3600*24*365, '/');
			}
			if ( isset($_GET['reset']) ) {
				resetStyle();
				echo "<div id=\"otherTip\">Your styles have been reseted!</div>";
			}
			if(isset($_POST['updatesInterval'])){
				setcookie('updatesInterval', $_POST['updatesInterval'], $_SERVER['REQUEST_TIME']+3600*24*365, '/');
				setcookie('intervalChanged', 'true', $_SERVER['REQUEST_TIME']+3600*24*365, '/');
			}
?>

<fieldset class="settings">
			<legend>桌面通知</legend>
				<div id ="isNotify">桌面通知支持状态:</div>
				<div id = "GetPer">
				<script type="text/javascript">
				function requestPermission(){
					var err = check_Notify_Env();
					if (err == -2)
					{
						getPermission();
						document.getElementById("isNotify").innerHTML = "桌面通知支持状态:支持,且拥有授权";
						document.getElementById("GetPer").innerHTML ="";
					}
				}
				var Notify_count =0;
				var err = check_Notify_Env();
				if(err == 0)
				document.getElementById("isNotify").innerHTML += "支持,且拥有授权";
				if(err == -1)
				document.getElementById("isNotify").innerHTML += "不支持";
				if(err == -2)
				{
					document.getElementById("isNotify").innerHTML += "支持,但是没有授权";
					document.getElementById("GetPer").innerHTML =  '<button id="requestPermission" onclick="requestPermission()">申请授权</button>';
				}
				</script>
			</fieldset>
		<form id="style_form" action="setting.php" method="post">

			<fieldset class="settings">

			<legend>预览设置</legend>

			<input id="showpic" type="checkbox" checked="checked" />
			<label>开启图片预览</label>

			<br /><br />

			<input id="mediaPreSelect" type="checkbox" checked="checked" />
			<label>开启视频预览</label>
			<br />

			</fieldset>

			<fieldset class="settings">

			<legend>浏览设置</legend>

			<label>主页刷新速度</label>
			<select id="homeInterval" name="homeInterval" value="<?php echo getCookie('homeInterval')?>">
				<option value="0.5">30 秒</option>
				<option value="1" selected="selected">1 分(默认)</option>
				<option value="3">3 分</option>
				<option value="5">5 分</option>
				<option value="10">10 分</option>
				<option value="0">不刷新</option>
			</select>
			&nbsp;&nbsp;
			<label>"更新页面"刷新速度</label>
			<select id="updatesInterval" name="updatesInterval" value="<?php echo getCookie('updatesInterval')?>">
				<option value="0.5">30 秒</option>
				<option value="1">1 分</option>
				<option value="3" selected="selected">3 分(默认)</option>
				<option value="5">5 分</option>
				<option value="10">10 分</option>
				<option value="0">不刷新</option>
			</select>
			<br /><br />

			<label>字体大小</label>
			<select id="fontsize" name="fontsize" value="<?php echo getCookie('fontsize')?>">
				<option value="12px">小</option>
				<option value="13px" selected="selected">适中(默认)</option>
				<option value="14px">大</option>
				<option value="15px">巨大</option>
			</select>
			<small>设置字体的大小</small>
	</fieldset>
    <table>
			<tr>
			<td colspan="2">
			<input type="submit" class="btn" id="save_button" value="保存" />
			<a id="reset_link" href="setting.php?reset=true" title="You will lose all customized settings!">回复到默认设置</a>
			</td>
			</tr>
			</table>
		</form>
			
<!--  ------------------------------------------------------------------------------------------------------------------ !-->
            <fieldset class="settings">

			<legend>风格设置</legend>

			<label>上传背景</label>
            <form action="setting.php?t=background" method="POST" enctype="multipart/form-data">
            <input type="file" name="image" id="bkimg" />
            <input type="submit" class="btn" id="background_button" value="上传" />
            </form>
            <script language="javascript">
				bkimg.addEventListener('change',readFile,false );
				function readFile(){
        			var file = this.files[0];
        			if(!/image\/\w+/.test(file.type)){
               		 alert("请确保文件为图像类型");
                	return false;
        			}
       				 var reader = new FileReader();
        				reader.readAsDataURL(file);
        				reader.onload = function(e){
						$(document.body).css("background", "url('"+this.result+"') fixed");
       					}
					}				
			</script>
			<br /><br />

			<label>页面配色</label>
     <form id="color_form" action="setting.php?t=color" method="post">
           <table width="292" border="0">
  <tr>
    <td width="102">背景颜色</td>
    <td width="180"><input class="bg_input" type="text" id="profile_background_color" name="profile_background_color" value="<?php echo $user->profile_background_color; ?>" /></td>
  </tr>
  <tr>
    <td>文字颜色</td>
    <td><input class="bg_input" type="text" id="profile_text_color" name="profile_text_color" value="<?php echo $user->profile_text_color; ?>" /></td>
  </tr>
  <tr>
    <td>连接颜色</td>
    <td><input class="bg_input" type="text" id="profile_link_color" name="profile_link_color" value="<?php echo $user->profile_link_color; ?>" /></td>
  </tr>
  <tr>
    <td>则栏颜色</td>
    <td><input class="bg_input" type="text" id="profile_sidebar_fill_color" name="profile_sidebar_fill_color" value="<?php echo $user->profile_sidebar_fill_color; ?>" /></td>
  </tr>
  <tr>
    <td>则栏边框颜色</td>
    <td><input class="bg_input" type="text" id="profile_sidebar_border_color" name="profile_sidebar_border_color" value="<?php echo $user->profile_sidebar_border_color; ?>" /></td>
  </tr>
</table>
<input type="submit" class="btn" id="color_button" value="更新" />
</form>
</fieldset>
		
  <!--- ----------------------------------------------------------------------------------------------- ---------------- !-->
<?php
	} // end switch
?>

	</div>
</div>

<?php 
	include ('inc/sidebar.php');
?>

<?php 
	include ('inc/footer.php');
?>
